package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.HeartData;

import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class HeartImpl implements HeartDao {
    @Override
    public List<HeartData> findAll(String site) {
        return DbHelp.getQueryAll(HeartData.class);
    }

    @Override
    public HeartData findById(String site, int lockid) {
        QueryBuilder qb = new QueryBuilder(HeartData.class).whereEquals(HeartData.LOCKID, lockid);
        List<HeartData> list = DbHelp.liteOrm.query(qb);
        return 0 == list.size() ? null : list.get(0);
    }

    @Override
    public void addAll(List<HeartData> heartDatas) {
        DbHelp.insertAll(heartDatas);
    }

    @Override
    public void addEvent(HeartData heartData) {
        HeartData data = findById(heartData.getSite(), Integer.valueOf(heartData.getLockid()));
        if (data == null)
            DbHelp.insert(heartData);
        else {
            data.setBlacklist(heartData.getBlacklist());
            data.setTimezone(heartData.getTimezone());
            data.setCalendar(heartData.getCalendar());
            data.setCardtime(heartData.getCardtime());
            data.setDst(heartData.getDst());
            data.setOperator(heartData.getOperator());
            data.setCardusertime(heartData.getCardusertime());
            data.setKeyusertime(heartData.getKeyusertime());
            data.setLockusertime(heartData.getLockusertime());
            DbHelp.update(data);
        }
    }

    @Override
    public void deleteAll(List<HeartData> eventData) {
        DbHelp.delete(eventData);
    }

    @Override
    public void deleteAll() {
        DbHelp.deleteAll(HeartData.class);
    }
}
